package indi.kider.server.appclient.service;

import indi.kider.common.constant.StatusCons;
import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.bill.Bill;
import indi.kider.common.model.bill.BillPay;
import indi.kider.server.appclient.model.TenantBillRequest;
import indi.kider.server.appclient.service.related.BillService;
import indi.kider.server.appclient.service.related.TenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

/**
 * Created by shixj on 2018/5/14.
 */
@Service
@Slf4j
public class TenantBillServiceImpl implements TenantBillService {
    @Autowired
    private TenantService tenantService;
    @Autowired
    private BillService billService;

    /**
     * 房客获取相关全部账单
     *
     * @param request
     * @return
     */
    @Override
    public CommonResponse getBillsPage(PageRequest<TenantBillRequest> request){
        Rent rent = tenantService.getTenantRent(request.getQuery().getPhone()).getData();
        Bill query = new Bill();
        if(request.getQuery().getPayState()>-1)
        query.setPayState(request.getQuery().getPayState());
        if(request.getQuery().getBillType()>-1)
            query.setBillType(request.getQuery().getBillType());
        if(rent!=null)
        query.setBillAttribution(rent.getId());
        else query.setBillAttribution(-1L);
        query.setState(StatusCons.VALID);
        PageRequest<Bill> pageRequest = new PageRequest();
        pageRequest.setQuery(query);
        pageRequest.setNum(request.getNum());
        pageRequest.setSize(request.getSize());
        return billService.query(pageRequest);
    }

    /**
     * 房客根据获取相关账单详情
     *
     * @param billId
     * @return
     */
    @Override
    public CommonResponse getBillDetail(Long billId) {
        return billService.queryById(billId);
    }

    /**
     * 房客获取未缴合计
     *
     * @param request
     * @return
     */
    @Override
    public CommonResponse getBillsTotal(TenantBillRequest request) {
        Rent rent = tenantService.getTenantRent(request.getPhone()).getData();
        return billService.getTotalUnpayed(rent.getId());
    }

    /**
     * 房客缴费更新账单缴费详情
     *
     * @param billPay
     * @return
     */
    @Override
    public CommonResponse updatePaystate(BillPay billPay) {
        return billService.updatePayStatus(billPay);
    }

    /**
     * 房客删除已缴费账单
     *
     * @param billId
     */
    @Override
    public CommonResponse delBill(Long billId) {
        return billService.updateState(billId);
    }
}
